﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Administration;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace QuickDeployEnhancements
{
    public class QuickDeployDoc : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {
        /// <summary>
        /// Add a listitem as QuickDeployObject.
        /// </summary>
        /// <param name="siteUrl">Url of the SiteCollection of the SubWeb of the ListItem</param>
        /// <param name="webUrl">Url of the SubWeb of the ListItem</param>
        /// <param name="itemUrl">Url of the ListItem</param>
        public void AddToQuickDeployment(string siteUrl, string webUrl, string itemUrl)
        {
            // to get web.allowUnsafeUpdates to work consitently
            // I had to trick it by setting the current HttpContext = null;
            // The underlying object model will set AllowUnsafeUpdates to true
            // if it thinks it's not coming from a web context.
            // So, I store the context in "o" then put it back when I'm done, below.
            object o = System.Web.HttpContext.Current;
            try
            {
                System.Web.HttpContext.Current = null;

                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.OpenWeb(webUrl))
                    {
                        web.AllowUnsafeUpdates = true;
                        ContentDeploymentJob.AddQuickDeployObject(web, Microsoft.SharePoint.Deployment.SPDeploymentObjectType.ListItem, itemUrl);
                        web.AllowUnsafeUpdates = false;
                        web.Update();
                    }
                }
            }
            finally
            {
                // put the web context back
                System.Web.HttpContext.Current = (System.Web.HttpContext)o;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string siteUrl = Request["SiteUrl"];
                string webUrl = SPContext.Current.Web.Url;
                string itemUrl = Request["ItemUrl"];

                webUrl = siteUrl.Remove(0, webUrl.Length);

                AddToQuickDeployment(siteUrl, webUrl, itemUrl);

                ClientScript.RegisterClientScriptBlock(this.GetType(), "GoBack", "function GoBack(){document.location='" + Request["returnurl"].ToString() + "';}", true);
            }
            catch (Exception ex)
            {
                throw new Exception("Could not save document to Quick Deploy job. " + ex.ToString());
            }
        }

        protected override void RenderChildren(System.Web.UI.HtmlTextWriter writer)
        {
            try
            {
                string siteUrl = Request["SiteUrl"];
                string webUrl = SPContext.Current.Web.Url;
                string itemUrl = Request["ItemUrl"];

                webUrl = siteUrl.Remove(0, webUrl.Length);

                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.OpenWeb(webUrl))
                    {
                        SPListItem item = web.GetListItem(itemUrl);
                        /*HyperLink link = new HyperLink();
                        link.NavigateUrl = item.Url;
                        link.Text = item.Title ;
                        Master.FindControl("PlaceHolderTitleBreadcrumb").FindControl("ContentMap").Controls.AddAt(0, link);*/

                        //Master.FindControl("PlaceHolderTitleBreadcrumb").FindControl("ContentMap").Controls.AddAt(0, new LiteralControl(" > "));
                        HyperLink link = new HyperLink();
                        link.NavigateUrl = item.ParentList.DefaultViewUrl;
                        link.Text = item.ParentList.Title;
                        Master.FindControl("PlaceHolderTitleBreadcrumb").FindControl("ContentMap").Controls.AddAt(0, link);

                        SPWeb w = item.ParentList.ParentWeb;
                        while (w != null)
                        {
                            Master.FindControl("PlaceHolderTitleBreadcrumb").FindControl("ContentMap").Controls.AddAt(0, new LiteralControl(" > "));
                            link = new HyperLink();
                            link.NavigateUrl = w.ServerRelativeUrl;
                            link.Text = w.Title;
                            Master.FindControl("PlaceHolderTitleBreadcrumb").FindControl("ContentMap").Controls.AddAt(0, link);
                            w = w.ParentWeb;
                        }
                    }
                }
            }
            catch
            {
                // Unable to show breadcrumb. Pity. But don't throw error on this.
            }

            base.RenderChildren(writer);
        }
    }
}
